﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TugasAkhir.Models;
using TugasAkhir.Objects;

namespace TugasAkhir
{
    public partial class PengaturanPertanyaan : System.Web.UI.Page
    {
        private TanggalObject tanggal;
        private string tahunSekarang;
        private string periodeSekarang;

        private DataSet pertanyaan;
        private DosenObject dosen;

        protected void Page_Load(object sender, EventArgs e)
        {
            CheckSession();
            CheckRole();

            TanggalModel.GetServerWaktu(out tanggal);
            this.Cek();

            if (!Page.IsCallback)
            {
                this.InitDropDownPertanyaan();
                this.InitTabelPertanyaan();
            }
        }

        private void CheckRole()
        {
            if (int.Parse(Session["id_role"].ToString().Substring(2)) == 2)
            {
                this.liPertanyaan.Attributes.Remove("hidden");
                this.liPengaturanPertanyaan.Attributes.Remove("hidden");
                this.liAnalisis.Attributes.Remove("hidden");
                this.liRekomendasi.Attributes.Remove("hidden");
            }
            else if (int.Parse(Session["id_role"].ToString().Substring(2)) == 3)
            {
                this.liAnalisis.Attributes.Remove("hidden");
                this.liRekomendasi.Attributes.Remove("hidden");
            }
            else
            {
                this.liRekomendasi.Attributes.Remove("hidden");
                this.liAnalisis.Attributes.Remove("hidden");
                this.liPengaturanRekomendasi.Attributes.Remove("hidden");
                this.liDaftarRekomendasi.Attributes.Remove("hidden");
            }
        }

        private void CheckSession()
        {
            if (Session["id_role"] != null && Session["id_role"].ToString() == "RL02")
            {
                DosenModel.GetDosenDetail(Session["username"].ToString(), out this.dosen);
                this.litName.Text = dosen.NamaDosen;
            }
            else
            {
                Response.Redirect("/Login.aspx");
            }
        }

        private void InitTabelPertanyaan()
        {
            bool flag = DetailPertanyaanModel.GetSemuaDetailPertanyaan(periodeSekarang, tahunSekarang, out this.pertanyaan);

            if (flag)
            {
                this.gvPengaturanPertanyaan.DataSource = this.pertanyaan.Tables[0];
                this.gvPengaturanPertanyaan.DataBind();
            }
        }

        private void InitDropDownPertanyaan()
        {
            var data = new DataSet();
            bool flag = PertanyaanModel.GetSemuaPertanyaan(out data);

            if (flag)
            {
                foreach (DataRow item in data.Tables[0].Rows)
                {
                    this.ddlPertanyaan.Items.Add(new ListItem(item[1].ToString(), item[0].ToString()));
                }
            }
        }

        private void Cek()
        {
            this.periodeSekarang = (this.tanggal.Bulan >= 1 && this.tanggal.Bulan <= 6) ? "GENAP" : "GASAL";
            this.tahunSekarang = String.Format("{0}-{1}", this.tanggal.Tahun, this.tanggal.Tahun + 1);

            this.litKeterangan.Text = String.Format("Tahun Ajaran: {0} Periode: {1}", this.tahunSekarang, this.periodeSekarang);
        }

        protected void btAdd_ServerClick(object sender, EventArgs e)
        {
            bool flag = DetailPertanyaanModel.TambahPertanyaan(this.selectedId.Value, this.tahunSekarang, this.periodeSekarang);

            if (flag)
            {
                this.InitTabelPertanyaan();
            }
        }

        protected void btDeletePertanyaan_Click(object sender, EventArgs e)
        {
            bool flag = DetailPertanyaanModel.HapusDetailPertanyaan(this.periodeSekarang, this.tahunSekarang, this.hfDeleteID.Value);

            if (flag)
            {
                this.InitTabelPertanyaan();
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "HideAddScript", "HideDelete();", true);
            }
        }

        protected void gvPengaturanPertanyaan_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("deleteRecord"))
            {
                int index = Convert.ToInt32(e.CommandArgument);
                this.DeleteCommand(index);
            }
        }

        protected void SetHiddenField(int index, ref HiddenField hiddenField)
        {
            string code = gvPengaturanPertanyaan.DataKeys[index].Value.ToString();
            hiddenField.Value = code;
        }

        private void DeleteCommand(int index)
        {
            this.SetHiddenField(index, ref this.hfDeleteID);

            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowDeleteScript", "ShowDelete();", true);
        }

        protected void gvPengaturanPertanyaan_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.gvPengaturanPertanyaan.PageIndex = e.NewPageIndex;
            this.InitTabelPertanyaan();

            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "InitScript", "Init();", true);
        }

        protected void lnkLogout_Click(object sender, EventArgs e)
        {
            Session["id_role"] = "";
            Session["username"] = "";
            CheckSession();
        }
    }
}